import pymongo

# 连接mongo数据库
client = pymongo.MongoClient('localhost', 27017)
# 获取数据库
# db = client.school
db = client['school']
# 获取要操作的集合
# collection = db.student
collection = db['student']

# 插入单条数据
# stu = {'name': '李四', 'age:': 24, 'gender': '男'}
# collection.insert_one(stu)

# 批量插入数据
# lst = [
#     {'name': '柯要', 'age:': 24, 'gender': '女'},
#     {'name': '优宁', 'age:': 20, 'gender': '女'},
#     {'name': '罗丽', 'age:': 27, 'gender': '女'},
#     {'name': '宏兕', 'age:': 18, 'gender': '男'},
# ]
# collection.insert_many(lst)


# 修改单条数据
# collection.update_one({'name': '李四'}, {'$set': {'age': 20}})
# 批量修改
# collection.update_many({'name': '赵珊珊'}, {'$set': {'gender': '女', 'age': 18}})

# 删除单条数据
# collection.delete_one({'name': '赵珊珊'})
# 批量删除
# collection.delete_many({'name': '赵珊珊', 'num': {'$gt': 20}})
# 查询
# 查询全部
# result = collection.find()
# for item in result:
#     print(item)
# 带条件查询
# result = collection.find({'name': '赵珊珊', 'num': {'$gte': 15}})
# for item in result:
#     print(item)
# 模糊查询
# result = collection.find({'name': {'$regex': '.*李，*'}})
# for item in result:
#     print(item)
# 排序 ASCENDING 升序  DESCENDING 降序
# result = collection.find().sort('age', pymongo.DESCENDING)
# for item in result:
#     print(item)
# 分页 跳过 3 条查询3条
result = collection.find().sort('age', pymongo.DESCENDING).skip(3).limit(3)
for item in result:
    print(item)
